import Vue from 'vue'
import App from './App.vue'
import router from './router'
import '@/vantui'
import 'vant/lib/index.css'


Vue.config.productionTip = false

let isPrd = location.href.includes('https://prd.uweiads.com');
let APP_ENV_CONFIG;
let isDev = !isPrd;
let alipay_app = ''
if (process.env.VUE_APP_FLAG === 'test') {
	const Vconsole = require('vconsole');
	const vconsole = new Vconsole();
	Vue.use(vconsole);
	APP_ENV_CONFIG = 'wxf80deec18d1bdc19'// 小春通讯
	alipay_app = '2021001146631427'// 小春通讯
}else {
	APP_ENV_CONFIG = 'wxf80deec18d1bdc19'// 小春通讯
	alipay_app = '2021001146631427'// 小春通讯
}

Vue.filter('formatDate', function(date) {
	if (!date) {
		return '';
	}
	return  date.replace('T',' ').substring(0,16)
})

Vue.filter('cellphone',function(iphone){
	if(!iphone){
		return ''
	}
	return iphone.substring(0,3)+'****'+iphone.substring(7)
})

Vue.filter('formatCardNumber',function(data){
	return data.substring(data.length-4)
})

Vue.filter('userRoleLevel',function(data){
	let map = new Map([
		['2-0','会员'],
		['2-1584','VIP'],
		['2-80','社长'],
		['2-81','合伙人'],
		['2-82','联合创始人'],
		['2-1741','经理'],
		['2-1740','经销商'],
		['2-1742','经销商'],
		['2-116','平台方'],
	])
	return map.get(data)
})
Vue.filter('amountFilter',function(num){
	return parseFloat(num).toFixed(2)
})

/*本地设备类型*/
let u = navigator.userAgent

if (u.indexOf('Android') > -1 || u.indexOf('Adr') > -1) {
  localStorage.setItem('device', 'android')
} else if (!!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)) {
  localStorage.setItem('device', 'ios')
} else {
  localStorage.setItem('device', 'pc')
}




window.weixin = {
    
	appId:APP_ENV_CONFIG,
	alipayApp:alipay_app,
	isAndroid:function(){
		return localStorage.getItem('device') == 'android'
	},
	login:function(){
		location.href = location.href.split('#')[0]+'#/login'
	},
	getUserId:function(){
		let token = null;
		if(weixin.isAndroid()){
			token = window.JS_TROOPS.getValue('fttToken')
		}else {
			token = localStorage.getItem('fttToken')
		}
		if(!token){
			return false
		}
		let obj = JSON.parse(token)
		return obj.userId
	},
	setItemToken:function(token){
		let token2 = JSON.stringify(token)
		if(weixin.isAndroid()){
			window.JS_TROOPS.saveValue('fttToken',token2)

		}else{
			return localStorage.setItem('fttToken',token2)
		}
	},
	getToken:function(){
		let token = null;
		if(weixin.isAndroid()){
			token = window.JS_TROOPS.getValue('fttToken')
		}else {
			token = localStorage.getItem('fttToken')
		}
		
		if(!token){
			return false
		}
		let obj = JSON.parse(token)
		return obj.accesstoken
	},
	removeToken:function(){
		if(weixin.isAndroid()){
			window.JS_TROOPS.deleteValue('fttToken')

		}else{
			return localStorage.removeItem('fttToken')
		}
	},
	
	uploadIF(file,funBack){
		var that = this
		var filePath = file.file.name
		var fileFormat = filePath.split('.')[1].toLowerCase();
		if(!fileFormat.match(/png|jpg|jpeg/)){
			this.$Message.info('上传错误，文件格式必须是png、jpg、jpeg')
			return false
		}
		let reader = new FileReader()
		let file2 = file.file
		if(file2){
			if(file2.size >1000000){
			   
				let fileSize = parseFloat(parseInt(file2['size']/1024/1024)).toFixed(2)
				let base64 = null;
				reader.readAsDataURL(file2)
				reader.onload = (e)=>{
					let img = new Image()
					img.src = e.target.result
					img.onload = function(){
						let w = this.width > 1440 ? 1440 : this.width,
						h = this.width > 1440 ?
							   1440 * (this.height / this.width)
							:  this.height;
						let canvas = document.createElement('canvas')
						let ctx = canvas.getContext('2d')
						canvas.setAttribute('width',w)
						canvas.setAttribute('height',h)
						ctx.drawImage(this,0,0,w,h)
						
						if(fileSize<1){
							base64 = canvas.toDataURL(file2.type,1)
						}else if(fileSize >1 && fileSize<2){
							base64 = canvas.toDataURL(file2.type,0.8)
						}else {
							base64 = canvas.toDataURL(file2.type,0.5)
						}
						
						let f = weixin.base64URLtoFile(base64,'t.jpg')
						funBack(f)
						
					}
					
				}
				return false

			}else {
				reader.readAsDataURL(file2)
				reader.onload = (data)=>{funBack(file2)}
				return false
			}
		}
		
	},
	base64URLtoFile:function(base64URL,filename){
		var arr = base64URL.split(','),
		mime = arr[0].match(/:(.*?);/)[1],
		bstr = atob(arr[1]),
		n = bstr.length,
		u8arr = new Uint8Array(n);
		while(n--) {
			u8arr[n] = bstr.charCodeAt(n);
		}
		return new File([u8arr],filename,{type:mime});
	},
}

router.beforeEach((to,from,next)=>{
	if(to.meta.title){
		document.title = to.meta.title
	}

	var tokenStr = window.weixin.getToken()

	if(tokenStr){
		next()
	}else if(to.path == '/register'){
		next()
	}else if(to.path == '/agreement'){
		next()
	}else{
		if(to.path !== '/login'){
			next('/login')
		}else{
			next()
		}
	}
	
	//next()
})

new Vue({
    router,
	render: h => h(App),
}).$mount('#app')